import 'package:get/get.dart';
import 'package:nyh_motorcade_app/entity/contact_us_entity.dart';
import 'package:nyh_motorcade_app/http/http_address.dart';
import 'package:nyh_motorcade_app/http/http_manager.dart';
import 'package:nyh_motorcade_app/utils/toast_utils.dart';
import 'package:nyh_motorcade_app/widgets/multi_state/multi_state_widget.dart';

class ContactUsController extends GetxController {
  MultiState multiState = MultiState.loading;
  String servicePhone = "";
  String websiteAddress = "";
  String companyAddress = "";
  String exclusiveAdviser = "";
  String contactPhone = "";

  @override
  void onReady() {
    super.onReady();
    getContactUsInfo();
  }

  void onRetry() {
    multiState = MultiState.loading;
    update();
    getContactUsInfo();
  }

  void getContactUsInfo() {
    httpManager.post(
      Get.context,
      HttpAddress.GET_CONTACT_US_INFO,
      {},
      isToast: true,
      onSuccess: (data) {
        if (data == null) {
          ToastUtils.toast("请求失败");
          multiState = MultiState.fail;
          update();
          return;
        }

        ContactUsEntity contactUsEntity = ContactUsEntity().fromJson(data);
        servicePhone = contactUsEntity.servicePhone ?? "";
        websiteAddress = contactUsEntity.websiteAddress ?? "";
        companyAddress = contactUsEntity.companyAddress ?? "";
        exclusiveAdviser = contactUsEntity.exclusiveAdviser ?? "";
        contactPhone = contactUsEntity.contactPhone ?? "";

        multiState = MultiState.success;
        update();
      },
      onFail: (code, msg) {
        multiState = MultiState.fail;
        update();
      },
      onError: (msg) {
        multiState = MultiState.error;
        update();
      },
    );
  }
}
